package cn.tedu.ivos.application.service.impl;

import cn.tedu.ivos.application.mapper.ApplicationMapper;
import cn.tedu.ivos.application.pojo.dto.ApplicationQuery;
import cn.tedu.ivos.application.pojo.dto.ApplicationSaveParam;
import cn.tedu.ivos.application.pojo.entity.Application;
import cn.tedu.ivos.application.pojo.vo.ApplicationVO;
import cn.tedu.ivos.application.service.ApplicationService;
import cn.tedu.ivos.audit.mapper.AuditMapper;
import cn.tedu.ivos.audit.pojo.vo.AuditVO;
import cn.tedu.ivos.audit.service.AuditService;
import cn.tedu.ivos.base.enums.ApplicationStatusEnum;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringJoiner;

@Service
@Slf4j
@Transactional //开启事务，操作要么都成功，要么都失败
public class ApplicationServiceImpl implements ApplicationService {
    @Autowired
    private ApplicationMapper applicationMapper;
    @Autowired
    private AuditService auditService;
    @Autowired
    private AuditMapper auditMapper;
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private VehicleMapper vehicleMapper;
    @Override
    public void save(ApplicationSaveParam applicationSaveParam) {
        //将applicationSaveParam对象里面的参数赋值到Application对象中
        Application application = new Application();
        BeanUtils.copyProperties(applicationSaveParam,application);
        //状态，已发起，新增一开始就是已发起的状态
        application.setStatus(ApplicationStatusEnum.PENDING.getCode());
        application.setCreateTime(new Date());
        //新增申请表单
        applicationMapper.insert(application);
        //新增审批表单
        auditService.insertAudit(application);
    }

    @Override
    public List<ApplicationVO> selectApplication(ApplicationQuery applicationQuery) {
        List<ApplicationVO> applicationVOS = applicationMapper.selectApplication(applicationQuery);
        for (int i = 0; i < applicationVOS.size(); i++) {
            ApplicationVO applicationVO = applicationVOS.get(i);
            //将对applicationVO对象的操作进行封装
            assignAuditUserList(applicationVO);
        }
        //千万要记得返回
        return applicationVOS;
    }

    @Override
    public void cancel(Long id) {
        Application application = new Application();
        application.setId(id);
        //修改状态
        application.setStatus(ApplicationStatusEnum.CANCEL.getCode());
        applicationMapper.update(application);
        //删除对应审核表中的审核数据
        auditMapper.deleteByApplicationId(id);
    }

    @Override
    public void distribute(Long applicationId, Long vehicleId) {
        //更新申请表单数据
        Application application = new Application();
        application.setId(applicationId); //设置申请单的id
        application.setVehicleId(vehicleId); //分配车辆id
        //甚至申请单状态为“已分配车辆"
        application.setStatus(ApplicationStatusEnum.ALLOCATION.getCode());
        //更新时间
        application.setUpdateTime(new Date());
        applicationMapper.update(application);
        //更新车辆表数据
        Vehicle vehicle = new Vehicle();
        //设置车辆id
        vehicle.setId(vehicleId);
        //设置车辆状态 2 表示已占用
        vehicle.setStatus("2");
        vehicleMapper.update(vehicle);
    }

    @Override
    public void back(Long applicationId, Long vehicleId) {
        //申请表状态改成   已分配-->结束工单
        Application application = new Application();
        application.setId(applicationId); //设置申请单的id
        application.setStatus(ApplicationStatusEnum.END.getCode());
        //更新时间
        application.setUpdateTime(new Date());
        applicationMapper.update(application);
        //车辆表状态改成 占用-->空闲
        Vehicle vehicle = new Vehicle();
        //设置车辆id
        vehicle.setId(vehicleId);
        //设置车辆状态 2 表示已占用  1表示空闲
        vehicle.setStatus("1");
        vehicleMapper.update(vehicle);
    }

    private void assignAuditUserList(ApplicationVO applicationVO) {
        //查询审批人的信息
        List<AuditVO> auditVOS = auditMapper.selectAuditByApplicationId(applicationVO.getId());
        //审批人的id集合，审批人姓名的集合
        List<Long> auditUserIdList = new ArrayList<>();
        List<String> auditUsernameList = new ArrayList<>();
        //遍历auditVOS对象
        for (int i = 0; i < auditVOS.size(); i++) {
            AuditVO auditVO = auditVOS.get(i);
            //获取审批人userId
            Long userId = auditVO.getAuditUserId();
            //将userId赋值到auditUserIdList集合中
            auditUserIdList.add(userId);
            //查询用户信息UserMapper
            UserVO userVO = userMapper.selectById(userId);
            //用户不存在
            if(userVO==null)
                //跳出本次循环，进入下一次循环
                continue;
            //将userVo中username存入到auditUsernameList集合中
            auditUsernameList.add(userVO.getUsername());
        }
        //将准备好的审批人的id集合数据赋值给applicationVO
        applicationVO.setAuditUserIdList(auditUserIdList);
        //需要一个工具，进行值与值之间的拼接
        StringJoiner stringJoiner = new StringJoiner(",");
        for (String username : auditUsernameList) {
            stringJoiner.add(username);
        }
        //将拼接好的username的列表转成一个字符串，将string赋值到applicationVO对象中
        applicationVO.setAuditUsernameList(stringJoiner.toString());
    }
}
